package Servlet.Teacher;

import Service.Teacher.Imp.StuSumServiceImp;
import Service.Teacher.StuSumService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * Created by IntelliJ IDEA.
 * Author: Deng PengFei
 * Date: 2021/8/30
 * Time: 15:43
 */

@WebServlet(urlPatterns = {"/getChooseSum", "/getSum"},name = "StuSumServlet")
public class StuSumServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;


    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }


    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String url = request.getRequestURI();
        if ("/getChooseSum".equals(url)) {
            getChooseSum(request, response);
        } else if ("/getSum".equals(url)) {
            getSum(request, response);
        }

    }

    /**
     * 得到各阶段的完成人数
     */
    private void getSum(HttpServletRequest request, HttpServletResponse response) throws IOException {
        StuSumService stuSumService = new StuSumServiceImp();
        String TNO = request.getParameter("userName");
        Integer type = Integer.parseInt(request.getParameter("type"));
        String sum = stuSumService.getSumByTNO(TNO, type);
        PrintWriter out = response.getWriter();
        out.print(sum);
        out.close();
    }

    /**
     * 根据老师的编号来获取指导的学生的总数和已经选题的人数
     */
    private void getChooseSum(HttpServletRequest request, HttpServletResponse response) throws IOException {
        StuSumService stuSumService = new StuSumServiceImp();
        String TNO = request.getParameter("userName");
        String sum = stuSumService.getChooseSumByTNO(TNO);
        PrintWriter out = response.getWriter();
        out.print(sum);
        out.close();
    }

}
